package com.bawei.module_home.ui

import com.bawei.lib_common.base.BaseResponse
import com.bawei.module_home.data.HomeItem
import com.bawei.module_home.http.HomeApi
import com.bawei.module_home.http.HomeModel
import javax.inject.Inject

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.withContext

class HomeRepository @Inject constructor(
    @HomeModel.CommonMainApi
    private val homeApi: HomeApi
){
    // 2. 增强版本（直接解析 BaseResponse 状态）
    suspend fun getHomeListById(id: Int): Result<List<HomeItem>> {
        return try {
            val response = withContext(Dispatchers.IO) {
                homeApi.getHomeById(id)
            }
            // 检查服务端返回的状态码
            if (response.code == 200) {
                // 提取数据部分
                Result.success(response.data ?: emptyList())
            } else {
                // 服务端错误
                Result.failure(Exception(response.message))
            }
        } catch (e: Exception) {
            // 网络异常
            Result.failure(e)
        }
    }

}